package LeetCode._01_树;

import java.util.LinkedList;
import java.util.Queue;

public class _101_对称二叉树 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public boolean isSymmetric(TreeNode root) {
        return traver(root.left,root.right);
    }

    //递归解法
    boolean traver(TreeNode node1,TreeNode node2){
        if(node1 == null || node2 == null) return node1 == node2;
        if(node1.val!=node2.val) return false;

        return traver(node1.left,node2.right) && traver(node1.right,node2.left);
    }

    //使用层序遍历
    private boolean isSmymmetric(TreeNode root){
        if(root == null) return true;

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root.left);
        queue.offer(root.right);
        while(!queue.isEmpty()){
            TreeNode left = queue.poll();
            TreeNode right = queue.poll();

            if(left == null && right == null) continue;
            if(left == null || right == null) return false;
            if(left.val!=right.val) return false;

            queue.offer(left.left);
            queue.offer(right.right);

            queue.offer(left.right);
            queue.offer(right.left);
        }
        return true;
    }
}
